<template>
  <el-dialog
    :before-close="beforeClose"
    :visible.sync="show"
    :title="title"
    width="35%"
    @close="close"
    @closed="closed"
  >
    <el-form ref="myForm" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="real_name">
        <el-input
          v-model="form.real_name"
          type="text"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="id_card">
        <el-input
          v-model="form.id_card"
          type="text"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex">
          <el-option value="男">男</el-option>
          <el-option value="女">女</el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="联系方式" prop="mobile">
        <el-input
          v-model="form.mobile"
          type="text"
        />
      </el-form-item>

      <el-form-item label="关注原因">
        <el-input v-model="form.note" type="textarea" rows="5" maxlength="200" show-word-limit />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmitChek">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ssqOptions, streetOptions, adminOptions } from '@/list/data'
import { focusGet, focusCreate, focusUpdate } from '@/api/focus'
export default {
  name: 'Index',
  components: { },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'view'
    },
    data: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      show: false,
      ssqOptions,
      streetOptions,
      adminOptions,
      form: {

      },
      rules: {
        real_name: [{ required: true, message: '请选择姓名', trigger: 'blur' }],
        id_card: [{ required: true, message: '请输入身份证', trigger: 'blur' }],
        sex: [{ required: true, message: '请选择性别', trigger: 'change' }],
        mobile: [{ required: true, message: '请输入联系方式', trigger: 'blur' }]

      }
    }
  },
  computed: {
    title() {
      let text = ''
      switch (this.type) {
        case 'view':
          text = '查看'
          break
        case 'create':
          text = '新建'
          break
        case 'edit':
          text = '编辑'
          break
      }
      return text
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.show = val
        this.getData()
      }
    }
  },
  mounted() {
    this.show = this.visible
  },
  methods: {
    handleChange(value) {
      console.log(value)
    },
    getData() {
      console.log('获取数据')
      if (this.data.id > 0) {
        focusGet(this.data.id).then(res => {
          this.form = res.data
        })
      } else {
        this.form = {

        }
      }
    },
    handleClose() {
      console.log('点击关闭')
      this.beforeClose()
    },
    handleSubmitChek() {
      this.$refs['myForm'].validate((valid) => {
        if (valid) {
          this.handleSubmit()
        } else {
          return false
        }
      })
    },
    handleSubmit() {
      console.log('点击确定')
      if (this.form.id > 0) {
        focusUpdate(this.form).then(res => {
          this.$notify.success('保存成功')
          this.handleClose()
        })
      } else {
        focusCreate(this.form).then(res => {
          this.$notify.success('保存成功')
          this.handleClose()
        })
      }
      this.$emit('refresh')
      this.handleClose()
    },
    beforeClose() {
      console.log('关闭前的回调')
      this.show = false
      this.$nextTick(function() {
        this.$refs['myForm'].clearValidate()
      })
      this.$emit('update:visible', false)
    },
    close() {
      console.log('关闭的回调')
    },
    closed() {
      console.log('关闭动画结束后的回调')
    }
  }
}
</script>

<style scoped>
</style>
